package com.krmes.mes;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.krmes.mes.app.AppConfig;
import com.krmes.mes.app.AppContext;
import com.krmes.mes.app.AppManager;
import com.krmes.mes.util.TDevice;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.util.Properties;

import cz.msebera.android.httpclient.Header;

public class ConfigActivity extends Activity
        implements View.OnClickListener{
    private Button btn_confirm;
    private Button btn_exit;
    private EditText etServerIp;
    private EditText etServerPort;
    private TextView tv_appver ;
    private Button btn_appver ;
    private String versionName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(1);
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.activity_config);
        this.btn_confirm = ((Button)findViewById(R.id.btn_confirm));
        this.btn_confirm.setOnClickListener(this);
        this.btn_exit = ((Button)findViewById(R.id.btn_exit));
        this.btn_exit.setOnClickListener(this);
        this.etServerIp = ((EditText)findViewById(R.id.etServerIp));
        this.etServerPort = ((EditText)findViewById(R.id.etServerPort));
        this.tv_appver = ((TextView)findViewById(R.id.tv_appver));
        this.btn_appver = ((Button)findViewById(R.id.btn_appver));
        this.btn_appver.setOnClickListener(this);
        PackageManager pm = this.getPackageManager();
        PackageInfo pi;
        versionName = "";
        try {
            pi = pm.getPackageInfo(this.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        this.tv_appver.setText("当前版本："+versionName);
        initData();
    }

    @Override
    public void onClick(View paramView){
        switch(paramView.getId()) {
            case R.id.btn_appver:
            {

                ServerApi.checkAppVer("app",versionName ,new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        String url = "";
                        String msg = new String(responseBody);
                        if (msg.startsWith("1:")) {
                            AppContext.showToast("正在下载请等待");
                            //启动服务
                            url = msg.substring(2,msg.length());
                            Intent service = new Intent(ConfigActivity.this,UpdateService.class);
                            service.putExtra("url",url);
                            startService(service);
                        }else {
                            AppContext.showToast(msg.substring(2,msg.length()));
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable throwable) {
                        AppContext.showToast(throwable.getMessage());
                    }

                });

                break;
            }
            case R.id.btn_confirm:
            {
                if(validate()) {
                    Properties pp =  AppConfig.getAppConfig(this).get();//new Properties();
                    pp.setProperty("server_ip", etServerIp.getText().toString());
                    pp.setProperty("server_port", etServerPort.getText().toString());
                    AppConfig.getAppConfig(this).setProperties(pp);//?????????????
                    AppConfig.getAppConfig(this).initProperties(this);
                    ApiHttpClient.resetHost();
                    Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
                    //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//设置不要刷新将要跳转的界面
                    startActivity(intent);
                    finish();
                }
                break;
            }
            case R.id.btn_exit:
            {
                Process.killProcess(Process.myPid());
                ServerApi.logout(AppContext.loginCode, TDevice.getDeviceId(), new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                    }
                });
                break;
            }
        }
    }
    private void initData()
    {
        AppConfig  apConfig=AppConfig.getAppConfig(this);
        apConfig.initProperties(this);
        this.etServerIp.setText(apConfig.Server_IP);
        this.etServerPort.setText(apConfig.Server_Port);
        AppManager.getAppManager().addActivity(this);
    }

    private boolean validate()
    {
        if ((this.etServerIp.getText().toString().equals("")) || (this.etServerPort.getText().toString().equals("")))
        {
            Toast.makeText(this, "请填写完整数据", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
